package com.acme.domain.account;


import com.acme.exceptions.NoEnoughFundsException;
import com.acme.exceptions.OverDraftLimitExceededException;

import java.io.Serializable;

public class CheckingAccount extends AbstractAccount implements Serializable {

    private double overdraft;
    private double INIT_OVERDRAFT;

    @Override
    public void withdraw(final double amount) throws NoEnoughFundsException {
        double min = Math.min(amount, getBalance());
        super.withdraw(min);
        this.overdraft -= (amount - min);
        if (this.overdraft < 0) throw new OverDraftLimitExceededException(this, "from CheckingAccount");
    }

    @Override
    public void deposit(double amount) throws IllegalArgumentException {
        double min = Math.min(INIT_OVERDRAFT - overdraft, amount);
        overdraft += min;
        super.deposit(amount - min);
    }

    public double getCredit() {
        return INIT_OVERDRAFT - overdraft;
    }

    @Override
    public String toString() {
        String str = super.toString();
        return str.substring(0, str.length()-1).concat(", overdraft=" + overdraft + '}');
    }

    public CheckingAccount(final double amount, final double overdraft) {
        super(amount);
        this.overdraft = this.INIT_OVERDRAFT = overdraft;
    }

    public double getOverdraft() {
        return overdraft;
    }
}
